<!DOCTYPE html>
<html>
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>首页</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="../../js/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="../../js/jquery-3.3.1.min.js"></script>
    <style>
        html body{
            height: 100%;
            margin: 0;
            padding: 0;
        }
    </style>
</head>
<body>
<div id="app">
    <el-dialog
            title="用品分类"
            :visible.sync="dialogVisible"
            width="30%">
        <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="类别名称">
                <el-input v-model="form.classname"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="form.remark"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="add">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <el-button type="primary" @click="dialogVisible = true;status=1" plain>添加</el-button>
    <el-table
            :data="tableData"
            style="width: 100%"
            max-height="500">
        <el-table-column
                fixed
                prop="id"
                label="id"
                width="120">
        </el-table-column>
        <el-table-column
                prop="classname"
                label="类别名称"
                width="350">
        </el-table-column>
        <el-table-column
                prop="remark"
                label="备注"
                width="350">
        </el-table-column>
        </el-table-column>
        <el-table-column
                fixed="right"
                label="操作"
                width="120">
            <template slot-scope="scope">
                <el-button
                        @click.native.prevent="deleteRow(scope.$index, tableData)"
                        type="text"
                        size="small">
                    移除
                </el-button>
                <el-button
                        @click.native.prevent="updateRow(scope.$index, tableData)"
                        type="text"
                        size="small">
                    修改
                </el-button>
            </template>
        </el-table-column>
    </el-table>
</div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            dialogVisible: false,
            tableData:[],
            status:0,
            form: {
                id:'',
                remark: '',
                classname:''
            }
        },
        methods: {
            deleteRow(index, rows) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let vue= this;
                    $.ajax({
                        url:"http://localhost:8080/ssmzy05/classify/delete",
                        type:"post",
                        data:{
                          id:rows[index].id
                        },
                        dataType:"json",
                        success:function (data) {
                            if(data==true){
                                alert("存在使用情况的用品不能删除")
                            }else {
                                vue.$message({
                                    type: 'success',
                                    message: '删除成功!'
                                });
                                rows.splice(index, 1);
                            }
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            updateRow(index, rows){
                let vue = this;
                vue.dialogVisible = true;
                let row = rows[index]; // 获取指定索引处的数据对象
                for (let key in row) {
                    if (row.hasOwnProperty(key)) {
                        vue.form[key] = row[key]; // 将指定索引处的属性值逐一赋给 vue.form
                    }
                }
                vue.status = 2;
            },
            select(){
                let vue= this;
                $.ajax({
                    url:"http://localhost:8080/ssmzy05/classify/select",
                    type:"post",
                    dataType:"json",
                    success:function (data) {
                        vue.tableData=data;
                    }
                })
            },
            add(){
                let vue= this;
                vue.dialogVisible = false;
                if(vue.status==1){
                    $.ajax({
                        url:"http://localhost:8080/ssmzy05/classify/insert",
                        type:"post",
                        data: vue.form,
                        dataType:"json",
                        success:function (data) {
                            vue.select()
                        }
                    })
                }else {
                    $.ajax({
                        url:"http://localhost:8080/ssmzy05/classify/update",
                        type:"post",
                        data: vue.form,
                        dataType:"json",
                        success:function (data) {
                            vue.select()
                        }
                    })
                }
            }
        },
        mounted() {
            this.select();
        },
        watch: {
            dialogVisible(newValue) {
                if (!newValue) {
                    this.form = {
                       classname: '',
                        remark: ''
                    };
                }
            }
        },
    })
</script>
</html>